﻿using System;
using System.Linq;
using Nop.Core.Domain.Shipping;

namespace Nop.Core.Domain.Logistics
{
    public static class Extensions
    {
        public static void ExtractPlanNoteStringContent(this PlanNote planNote,
                    out PlanNoteStatus action, out PlanStatus planStatus,
                    out int customerId, out int? packageId,
                    out ShippingStatus? packageStatus, out int? addressId)
        {
            packageId = null;
            packageStatus = null;
            addressId = null;

            var splitNote = planNote.Note.Split(new char[] { '|' });

            if (splitNote.Count() < 3)
                throw new Exception(String.Format("PlanNote string content has too few fields: {0}", planNote.Note));

            var actionString = splitNote[0];
            action = PlanNoteCommon.StringToPlanNoteStatus(actionString);

            planStatus = (PlanStatus) Int32.Parse(splitNote[1]);
            customerId = Int32.Parse(splitNote[2]);

            if (splitNote.Count() > 3)
            {
                packageId = Int32.Parse(splitNote[3]);
                packageStatus = (ShippingStatus) Int32.Parse(splitNote[4]);
            }

            if (splitNote.Count() > 5)
            {
                int addrId;
                if (Int32.TryParse(splitNote[5], out addrId))
                {
                    addressId = addrId;
                }
            }
        }
    }
}
